class hotdata{
    storageName = '';
    size = 100;
    primarykey = '';
    data = [];
    constructor(storageName,primarykey,size){
        this.storageName = storageName;
        this.primarykey = primarykey;
        this.size = size;
        this.data = localStorage.getItem(storageName) ? JSON.parse(localStorage.getItem(storageName)) : [];
    }

    get(id){
        return this.data.find(item=>item[this.primarykey] == id);
    }

    set(data){
        
        if (typeof data !='object' || data==null) return;
        let index  = this.data.findIndex(item=>item[this.primarykey] == data[this.primarykey]);
        if (index !== -1){
            this.data.splice(index,1);
        }else{
            if (this.data.length >= this.size){
                this.data.shift();
            }
        }

        this.data.push(data);

        localStorage.setItem(this.storageName,JSON.stringify(this.data));
    }

    getList(){
        return this.data;
    }
}

export default hotdata;

